package com.bfly.mall.user.api;

import com.bfly.common.ResponseUtil;
import com.bfly.core.base.controller.BaseController;
import com.bfly.core.base.entity.ResponseData;
import com.bfly.core.security.ActionModel;
import com.bfly.core.security.Login;
import com.bfly.mall.user.entity.Address;
import com.bfly.mall.user.dto.AddressFormDTO;
import com.bfly.mall.user.dto.AddressListDTO;
import com.bfly.mall.user.service.IAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户收货地址Api
 *
 * @author 长沙商脉科技有限公司
 * @date 2020/9/18 21:08
 */
@RestController
@RequestMapping(value = "/api/address")
public class AddressApiController extends BaseController {

    @Autowired
    private IAddressService addressService;


    /**
     * 用户收货地址列表
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/9/18 21:10
     */
    @GetMapping(value = "/list")
    @Login
    public void list(HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        param.put("user_id", getUser().getId());
        List<Address> list = addressService.getList(param);
        List<AddressListDTO> addressList = new ArrayList<>();
        for (Address address : list) {
            AddressListDTO dto = new AddressListDTO();
            BeanUtils.copyProperties(address, dto);
            addressList.add(dto);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(addressList));
    }

    /**
     * 获取用户默认的收货地址
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/17 21:55
     */
    @GetMapping(value = "/default")
    @Login
    public void getDefaultAddress(HttpServletResponse response) {
        Address address = addressService.getDefaultAddress(getUser().getId());
        AddressListDTO dto = null;
        if (address != null) {
            dto = new AddressListDTO();
            BeanUtils.copyProperties(address, dto);
        }
        ResponseUtil.writeJson(response, ResponseData.getSuccess(dto));
    }

    /**
     * 添加收货地址
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/18 19:57
     */
    @PostMapping(value = "/add")
    @Login
    @ActionModel(value = "新增收货地址")
    public void addAddress(HttpServletResponse response, @Valid @RequestBody AddressFormDTO formDTO, BindingResult result) {
        validData(result);
        addressService.save(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 添加收货地址
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/18 19:57
     */
    @PostMapping(value = "/edit")
    @Login
    @ActionModel(value = "编辑收货地址")
    public void editAddress(HttpServletResponse response, @Valid @RequestBody AddressFormDTO formDTO, BindingResult result) {
        validData(result);
        addressService.edit(formDTO);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 设置默认地址
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/19 23:14
     */
    @GetMapping(value = "/setting/default")
    @Login
    @ActionModel(value = "设置默认收货地址")
    public void setDefaultAddress(HttpServletResponse response, @RequestParam("addressId") int addressId) {
        addressService.settingDefaultAddress(getUser().getId(), addressId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }

    /**
     * 删除收货地址
     *
     * @author 长沙商脉科技有限公司
     * @date 2020/11/19 23:18
     */
    @GetMapping(value = "/remove")
    @Login
    @ActionModel(value = "删除收货地址")
    public void removeAddress(HttpServletResponse response, @RequestParam("addressId") int addressId) {
        addressService.removeAddress(getUser().getId(), addressId);
        ResponseUtil.writeJson(response, ResponseData.getSuccess(""));
    }
}